<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="module-email.iterators.html" />
<link rel="prev" href="module-email.errors.html" />
<link rel="parent" href="module-email.html" />
<link rel="next" href="module-email.iterators.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>7.1.9 Miscellaneous utilities</title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="7.1.8 exception and Defect"
  href="module-email.errors.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="7.1 email  "
  href="module-email.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="7.1.10 Iterators"
  href="module-email.iterators.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-email.errors.html">7.1.8 Exception and Defect</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-email.html">7.1 email  </a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-email.iterators.html">7.1.10 Iterators</a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h2><a name="SECTION009190000000000000000">
7.1.9 Miscellaneous utilities</a>
</h2>
<a name="module-email.utils"></a>

<p>
There are several useful utilities provided in the <tt class="module">email.utils</tt>
module:

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-1314' xml:id='l2h-1314' class="function">quote</tt></b>(</nobr></td>
  <td><var>str</var>)</td></tr></table></dt>
<dd>
Return a new string with backslashes in <var>str</var> replaced by two
backslashes, and double quotes replaced by backslash-double quote.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-1315' xml:id='l2h-1315' class="function">unquote</tt></b>(</nobr></td>
  <td><var>str</var>)</td></tr></table></dt>
<dd>
Return a new string which is an <em>unquoted</em> version of <var>str</var>.
If <var>str</var> ends and begins with double quotes, they are stripped
off.  Likewise if <var>str</var> ends and begins with angle brackets, they
are stripped off.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-1316' xml:id='l2h-1316' class="function">parseaddr</tt></b>(</nobr></td>
  <td><var>address</var>)</td></tr></table></dt>
<dd>
Parse address - which should be the value of some address-containing
field such as <span class="mailheader">To:</span> or <span class="mailheader">Cc:</span> - into its constituent
<em>realname</em> and <em>email address</em> parts.  Returns a tuple of that
information, unless the parse fails, in which case a 2-tuple of
<code>('', '')</code> is returned.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-1317' xml:id='l2h-1317' class="function">formataddr</tt></b>(</nobr></td>
  <td><var>pair</var>)</td></tr></table></dt>
<dd>
The inverse of <tt class="method">parseaddr()</tt>, this takes a 2-tuple of the form
<code>(realname, email_address)</code> and returns the string value suitable
for a <span class="mailheader">To:</span> or <span class="mailheader">Cc:</span> header.  If the first element of
<var>pair</var> is false, then the second element is returned unmodified.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-1318' xml:id='l2h-1318' class="function">getaddresses</tt></b>(</nobr></td>
  <td><var>fieldvalues</var>)</td></tr></table></dt>
<dd>
This method returns a list of 2-tuples of the form returned by
<code>parseaddr()</code>.  <var>fieldvalues</var> is a sequence of header field
values as might be returned by <tt class="method">Message.get_all()</tt>.  Here's a
simple example that gets all the recipients of a message:

<p>
<div class="verbatim"><pre>
from email.utils import getaddresses

tos = msg.get_all('to', [])
ccs = msg.get_all('cc', [])
resent_tos = msg.get_all('resent-to', [])
resent_ccs = msg.get_all('resent-cc', [])
all_recipients = getaddresses(tos + ccs + resent_tos + resent_ccs)
</pre></div>
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-1319' xml:id='l2h-1319' class="function">parsedate</tt></b>(</nobr></td>
  <td><var>date</var>)</td></tr></table></dt>
<dd>
Attempts to parse a date according to the rules in <a class="rfc" id='rfcref-99392' xml:id='rfcref-99392'
href="http://www.faqs.org/rfcs/rfc2822.html">RFC 2822</a>.
however, some mailers don't follow that format as specified, so
<tt class="function">parsedate()</tt> tries to guess correctly in such cases. 
<var>date</var> is a string containing an <a class="rfc" id='rfcref-99394' xml:id='rfcref-99394'
href="http://www.faqs.org/rfcs/rfc2822.html">RFC 2822</a> date, such as 
<code>"Mon, 20 Nov 1995 19:12:08 -0500"</code>.  If it succeeds in parsing
the date, <tt class="function">parsedate()</tt> returns a 9-tuple that can be passed
directly to <tt class="function">time.mktime()</tt>; otherwise <code>None</code> will be
returned.  Note that fields 6, 7, and 8 of the result tuple are not
usable.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-1320' xml:id='l2h-1320' class="function">parsedate_tz</tt></b>(</nobr></td>
  <td><var>date</var>)</td></tr></table></dt>
<dd>
Performs the same function as <tt class="function">parsedate()</tt>, but returns
either <code>None</code> or a 10-tuple; the first 9 elements make up a tuple
that can be passed directly to <tt class="function">time.mktime()</tt>, and the tenth
is the offset of the date's timezone from UTC (which is the official
term for Greenwich Mean Time)<a name="tex2html61"
  href="#foot21833"><sup>7.3</sup></a>.  If the input
string has no timezone, the last element of the tuple returned is
<code>None</code>.  Note that fields 6, 7, and 8 of the result tuple are not
usable.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-1321' xml:id='l2h-1321' class="function">mktime_tz</tt></b>(</nobr></td>
  <td><var>tuple</var>)</td></tr></table></dt>
<dd>
Turn a 10-tuple as returned by <tt class="function">parsedate_tz()</tt> into a UTC
timestamp.  It the timezone item in the tuple is <code>None</code>, assume
local time.  Minor deficiency: <tt class="function">mktime_tz()</tt> interprets the
first 8 elements of <var>tuple</var> as a local time and then compensates
for the timezone difference.  This may yield a slight error around
changes in daylight savings time, though not worth worrying about for
common use.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-1322' xml:id='l2h-1322' class="function">formatdate</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>timeval</var><big>[</big><var>, localtime</var><big>]</big><var></var><big>[</big><var>, usegmt</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Returns a date string as per <a class="rfc" id='rfcref-99396' xml:id='rfcref-99396'
href="http://www.faqs.org/rfcs/rfc2822.html">RFC 2822</a>, e.g.:

<p>
<div class="verbatim"><pre>
Fri, 09 Nov 2001 01:08:47 -0000
</pre></div>

<p>
Optional <var>timeval</var> if given is a floating point time value as
accepted by <tt class="function">time.gmtime()</tt> and <tt class="function">time.localtime()</tt>,
otherwise the current time is used.

<p>
Optional <var>localtime</var> is a flag that when <code>True</code>, interprets
<var>timeval</var>, and returns a date relative to the local timezone
instead of UTC, properly taking daylight savings time into account.
The default is <code>False</code> meaning UTC is used.

<p>
Optional <var>usegmt</var> is a flag that when <code>True</code>, outputs a 
date string with the timezone as an ascii string <code>GMT</code>, rather
than a numeric <code>-0000</code>. This is needed for some protocols (such
as HTTP). This only applies when <var>localtime</var> is <code>False</code>.

<span class="versionnote">New in version 2.4.</span>

</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-1323' xml:id='l2h-1323' class="function">make_msgid</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>idstring</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Returns a string suitable for an <a class="rfc" id='rfcref-99398' xml:id='rfcref-99398'
href="http://www.faqs.org/rfcs/rfc2822.html">RFC 2822</a>-compliant
<span class="mailheader">Message-ID:</span> header.  Optional <var>idstring</var> if given, is
a string used to strengthen the uniqueness of the message id.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-1324' xml:id='l2h-1324' class="function">decode_rfc2231</tt></b>(</nobr></td>
  <td><var>s</var>)</td></tr></table></dt>
<dd>
Decode the string <var>s</var> according to <a class="rfc" id='rfcref-99400' xml:id='rfcref-99400'
href="http://www.faqs.org/rfcs/rfc2231.html">RFC 2231</a>.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-1325' xml:id='l2h-1325' class="function">encode_rfc2231</tt></b>(</nobr></td>
  <td><var>s</var><big>[</big><var>, charset</var><big>[</big><var>, language</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Encode the string <var>s</var> according to <a class="rfc" id='rfcref-99402' xml:id='rfcref-99402'
href="http://www.faqs.org/rfcs/rfc2231.html">RFC 2231</a>.  Optional
<var>charset</var> and <var>language</var>, if given is the character set name
and language name to use.  If neither is given, <var>s</var> is returned
as-is.  If <var>charset</var> is given but <var>language</var> is not, the
string is encoded using the empty string for <var>language</var>.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-1326' xml:id='l2h-1326' class="function">collapse_rfc2231_value</tt></b>(</nobr></td>
  <td><var>value</var><big>[</big><var>, errors</var><big>[</big><var>,
    fallback_charset</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
When a header parameter is encoded in <a class="rfc" id='rfcref-99404' xml:id='rfcref-99404'
href="http://www.faqs.org/rfcs/rfc2231.html">RFC 2231</a> format,
<tt class="method">Message.get_param()</tt> may return a 3-tuple containing the character
set, language, and value.  <tt class="function">collapse_rfc2231_value()</tt> turns this into
a unicode string.  Optional <var>errors</var> is passed to the <var>errors</var>
argument of the built-in <tt class="function">unicode()</tt> function; it defaults to
<code>replace</code>.  Optional <var>fallback_charset</var> specifies the character set
to use if the one in the <a class="rfc" id='rfcref-99406' xml:id='rfcref-99406'
href="http://www.faqs.org/rfcs/rfc2231.html">RFC 2231</a> header is not known by Python; it defaults
to <code>us-ascii</code>.

<p>
For convenience, if the <var>value</var> passed to
<tt class="function">collapse_rfc2231_value()</tt> is not a tuple, it should be a string and
it is returned unquoted.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-1327' xml:id='l2h-1327' class="function">decode_params</tt></b>(</nobr></td>
  <td><var>params</var>)</td></tr></table></dt>
<dd>
Decode parameters list according to <a class="rfc" id='rfcref-99408' xml:id='rfcref-99408'
href="http://www.faqs.org/rfcs/rfc2231.html">RFC 2231</a>.  <var>params</var> is a
sequence of 2-tuples containing elements of the form
<code>(content-type, string-value)</code>.
</dl>

<p>

<span class="versionnote">Changed in version 2.4:
The <tt class="function">dump_address_pair()</tt> function has been removed;
use <tt class="function">formataddr()</tt> instead.</span>

<p>

<span class="versionnote">Changed in version 2.4:
The <tt class="function">decode()</tt> function has been removed; use the
<tt class="method">Header.decode_header()</tt> method instead.</span>

<p>

<span class="versionnote">Changed in version 2.4:
The <tt class="function">encode()</tt> function has been removed; use the
<tt class="method">Header.encode()</tt> method instead.</span>


<p>
<br><hr><h4>Footnotes</h4>
<dl>
<dt><a name="foot21833">... Time)</a><A
 href="module-email.utils.html#tex2html61"><sup>7.3</sup></a></dt>
<dd>Note that the sign of the timezone
offset is the opposite of the sign of the <code>time.timezone</code>
variable for the same timezone; the latter variable follows the
POSIX standard while this module follows <a class="rfc" id='rfcref-99383' xml:id='rfcref-99383'
href="http://www.faqs.org/rfcs/rfc2822.html">RFC 2822</a>.

</dd>
</dl>
<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="7.1.8 exception and Defect"
  href="module-email.errors.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="7.1 email  "
  href="module-email.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="7.1.10 Iterators"
  href="module-email.iterators.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-email.errors.html">7.1.8 Exception and Defect</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-email.html">7.1 email  </a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-email.iterators.html">7.1.10 Iterators</a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
